package com.android.geotrack;
import java.util.ArrayList;

import com.android.geotrack.R;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;

/***
*
* Main class - Container.java
*/
public class Container extends TabActivity
{

	public static final String TAG_LOCATION_TAB = "LocationActivity"; 
    public static final String TAG_TOPTRACKS_TAB = "TopTracksActivity"; 
     
    public static final int ID_LOCATION_TAB = 0; 
    public static final int ID_TOPTRACKS_TAB = 1;
    
    // private TabHost host 
    
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		final TabHost host = getTabHost(); 
		//requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
		super.onCreate(savedInstanceState);
		
		host.addTab(host.newTabSpec(TAG_LOCATION_TAB)
				.setIndicator(TAG_LOCATION_TAB, getResources()
					.getDrawable(R.drawable.geotr))
				.setContent(new Intent(this, LocationActivity.class)));
		
		setText(ID_LOCATION_TAB, "", new Float(10.0),host);
		
		host.addTab(host.newTabSpec(TAG_TOPTRACKS_TAB)
				.setIndicator(TAG_TOPTRACKS_TAB,getResources()
					.getDrawable(R.drawable.track))
				.setContent(new Intent(this, TopTracksActivity.class)));

		setText(ID_TOPTRACKS_TAB, "", new Float(10.0),host); 
		
		host.setOnTabChangedListener(new OnTabChangeListener()
		{
			public void onTabChanged(String tabId)
			{
				if(tabId.equals(TAG_LOCATION_TAB))
				{
					setIcon(host.getCurrentTab(), R.drawable.geotr, host);
					setText(host.getCurrentTab(),"", new Float(10.0), host);
					setIcon(ID_TOPTRACKS_TAB, R.drawable.track,host);
					setText(ID_TOPTRACKS_TAB, "", new Float(10.0), host);
				}
				else if(tabId.equals(TAG_TOPTRACKS_TAB))
				{ 	
					setIcon(host.getCurrentTab(), R.drawable.track,host);
					setText(host.getCurrentTab(), "", new Float(10.0), host);
					setIcon(ID_LOCATION_TAB, R.drawable.geotr,host);
					setText(ID_LOCATION_TAB, "", new Float(10.0), host);
				}
			} 
		}); 
	}
	
	private void setIcon(int whichTab, int resourceId,TabHost host)
	{ 
		ArrayList <View> views = host.getTabWidget().getChildAt(whichTab).getTouchables(); 
		RelativeLayout relLayout = (RelativeLayout)views.get(0);
		ImageView iv = (ImageView)relLayout.getChildAt(0);
		iv.setImageDrawable(getResources().getDrawable(resourceId)); 
	} 
  
	private void setText(int whichTab, String text, Float size, TabHost host)
	{
		ArrayList<View> views = host.getTabWidget().getChildAt(whichTab).getTouchables(); 
		RelativeLayout relLayout = (RelativeLayout)views.get(0); 
		TextView tv = (TextView)relLayout.getChildAt(1); 
		if(text != null){ 
			tv.setText(text); 
        } 
		tv.setTextSize(size);
	}
}